package com.shenhonghui.twins;

import com.shenhonghui.twins.slice.PianoAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.bundle.AbilityInfo;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability implements IAbilityContinuation {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(PianoAbilitySlice.class.getName());
        this.setDisplayOrientation(AbilityInfo.DisplayOrientation.LANDSCAPE);
        requestPermission();
    }

    //获取权限
    private void requestPermission() {
        String[] permission = {
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.servicebus.ACCESS_SERVICE",
                "ohos.permission.WRITE_USER_STORAGE",
                "ohos.permission.DISTRIBUTED_DATASYNC",
                "ohos.permission.MICROPHONE",
                "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO",
                "ohos.permission.KEEP_BACKGROUND_RUNNING"};
        List<String> applyPermissions = new ArrayList<>();
        for (String element : permission) {
            if (verifySelfPermission(element) != 0) {
                if (canRequestPermission(element)) {
                    applyPermissions.add(element);
                }
            }
        }
        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);
    }

    @Override
    public boolean onStartContinuation() {
        System.out.println("开始迁移");
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        System.out.println("开始保存数据");
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        System.out.println("解压数据");
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {
        System.out.println("完成迁移");
    }

    @Override
    public void onRemoteTerminated() {

    }
}
